﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class DangKi_DAL
    {
          private DataProvider _provider = new DataProvider();
	
		    public DangKi_DAL()
		    {
			    _provider.connect();
		    }
            public List<DangKi_DTO> DS_DangKiTheoMaHS(int MaHS)
            {
                // Một list các parameter
                List<SqlParameter> paramters = new List<SqlParameter>();
                paramters.Add(new SqlParameter("@MaHS", MaHS));



                // Gọi hàm excute sử dujg procedure có parameter
                SqlDataReader reader = (SqlDataReader)_provider.executeQueryParameter("DS_DangKiTheoMaHS", paramters);

                List<DangKi_DTO> List_DK = new List<DangKi_DTO>();

                while (reader.Read())
                {
                    DangKi_DTO dk = new DangKi_DTO();
                    dk.MaDV = reader["MaDV"].ToString();
                    dk.MaHS = int.Parse(reader["MaHS"].ToString());
                    dk.Tungay = Convert.ToDateTime(reader["Tungay"]);
                    dk.Denngay = Convert.ToDateTime(reader["Denngay"]);



                    List_DK.Add(dk);

                }

                reader.Close();
                return List_DK;
            }
            public void ThemDangKi(DangKi_DTO dkdv)
            {
                string insertCommand = @"INSERT INTO DANGKI  VALUES('" +
                    dkdv.MaDV + "'," + dkdv.MaHS + ",'" + dkdv.Tungay + "','" + dkdv.Denngay + "')";

                _provider.executeNonQuery(insertCommand);
            }
            public List<DangKi_DTO> DS_DangKiDV()
            {
                SqlDataReader reader = (SqlDataReader)_provider.executeQuery("DS_DangKiDV");
                List<DangKi_DTO> List_DKDV = new List<DangKi_DTO>();

                while (reader.Read())
                {
                    DangKi_DTO dkdv = new DangKi_DTO();
                    dkdv.MaDV = reader["MaDV"].ToString();
                    dkdv.MaHS = int.Parse(reader["MaHS"].ToString());
                    dkdv.Tungay = Convert.ToDateTime(reader["Tungay"]);
                    dkdv.Denngay = Convert.ToDateTime(reader["Denngay"]);



                    List_DKDV.Add(dkdv);

                }

                reader.Close();
                return List_DKDV;
            }
            public List<DangKi_DTO> DS_DangKitheoMaDV(string MaDV)
            {
                // Một list các parameter
                List<SqlParameter> paramters = new List<SqlParameter>();
                paramters.Add(new SqlParameter("@MaDV", MaDV));

                //string query = "SELECT * FROM HOCSINH WHERE MAHS LIKE '%"+ MaHS +"%'";

                // Gọi hàm excute sử dujg procedure có parameter
                SqlDataReader reader = (SqlDataReader)_provider.executeQueryParameter("DS_DangKiTheoMaDV", paramters);

                List<DangKi_DTO> List_HS = new List<DangKi_DTO>();

                while (reader.Read())
                {
                    DangKi_DTO dkdv = new DangKi_DTO();
                    dkdv.MaDV = reader["MaDV"].ToString();
                    dkdv.MaHS = int.Parse(reader["MaHS"].ToString());
                    dkdv.Tungay = Convert.ToDateTime(reader["Tungay"]);
                    dkdv.Denngay = Convert.ToDateTime(reader["Denngay"]);


                    List_HS.Add(dkdv);
                }

                reader.Close();
                return List_HS;
            }



            public void Xoa(DangKi_DTO dkdv)
            {
                string deleteCommand1 = "DELETE FROM DANGKI WHERE MaDV = '" + dkdv.MaDV + "'";
                
                _provider.executeNonQuery(deleteCommand1);

            }

    }
}
